package com.stable.spider.eastmoney;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.stable.utils.DateUtil;
import com.stable.utils.HttpUtil;
import com.stable.utils.MathUtil;
import com.stable.utils.TradeDateUtil;
import com.stable.vo.EastHsgtcg;
import com.stable.vo.EastHsgtcgDetail;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Log4j2
public class EastHsgtcgSpider {

    /**
     * #沪深港通持股 占流通股本比
     * sort ShareSZ=市值 LTZB=占流通股比 ZZB=占总股本比
     * http://data.eastmoney.com/hsgtcg/list.html
     * http://dcfm.eastmoney.com/EM_MutiSvcExpandInterface/api/js/get?type=HSGT20_GGTJ_SUM&token=894050c76af8597a853f5b408b759f5d&st=LTZB&sr=-1&p=1&ps=50&filter=(DateType=%271%27%20and%20HdDate=%272020-06-16%27)&rt=53079759
     *
     * @return
     */
    public List<EastHsgtcg> getHSGTCG(int size, String datetime, String sort) {
        String basicUrl = "http://dcfm.eastmoney.com/EM_MutiSvcExpandInterface/api/js/get?type=HSGT20_GGTJ_SUM&token=894050c76af8597a853f5b408b759f5d&st=" + sort + "&sr=-1&p=1&ps=" + size + "&filter=(DateType=%271%27%20and%20HdDate=%27" + datetime + "%27)&rt=53079759";
        //String url = basicUrl.replace("(","%28").replace(")","%29");
        String result = HttpUtil.doGet2(basicUrl);

        List<EastHsgtcg> list = new ArrayList<>();
        JSONArray array = JSONArray.parseArray(result);
        if (array.size() == 0) {
            return getHSGTCG(size,  DateUtil.convertDate(DateUtil.getDatetimeBeforNDayYYYYMMDD(DateUtil.convertDate2(datetime), 1)), sort);
        }
        for (int i = 0; i < array.size(); i++) {
            EastHsgtcg ext = new EastHsgtcg();
            JSONObject jsonObject = array.getJSONObject(i);
            ext.setCode(jsonObject.getString("SCode"));
            ext.setName(jsonObject.getString("SName"));
            ext.setHyname(jsonObject.getString("HYName"));
            ext.setDqname(jsonObject.getString("DQName"));
            ext.setNewPrice(jsonObject.getString("NewPrice"));
            ext.setZdf(jsonObject.getString("Zdf"));
            ext.setShareHold(MathUtil.formatNumWanYi(jsonObject.getString("ShareHold")));
            ext.setShareSZ(MathUtil.formatNumWanYi(jsonObject.getString("ShareSZ")));
            ext.setLtzb(MathUtil.formatNumBFB(jsonObject.getString("LTZB")));
            ext.setZzb(MathUtil.formatNumBFB(jsonObject.getString("ZZB")));
            ext.setShareHold_Chg_One(MathUtil.formatNumWanYi(jsonObject.getString("ShareHold_Chg_One")));
            ext.setShareSZ_Chg_One(MathUtil.formatNumWanYi(jsonObject.getString("ShareSZ_Chg_One")));
            ext.setShareSZ_Chg_Rate_One(MathUtil.formatNumBFB(jsonObject.getString("ShareSZ_Chg_Rate_One")));
            ext.setLtzb_One(MathUtil.formatNumBFB(jsonObject.getString("LTZB_One")));
            ext.setZzb_One(MathUtil.formatNumBFB(jsonObject.getString("ZZB_One")));

            list.add(ext);
        }
        return list;
    }


    //

    /**
     * #沪深港通持股 购买每日明细
     * sort
     * http://data.eastmoney.com/hsgtcg/StockHdStatistics.aspx?stock=603799
     * http://dcfm.eastmoney.com/em_mutisvcexpandinterface/api/js/get?type=HSGTHDSTA&token=70f12f2f4f091e459a279469fe49eca5&filter=(SCODE=%27603799%27)&st=HDDATE&sr=-1&p=1&ps=50&rt=53109038
     *
     * @return
     */
    public List<EastHsgtcgDetail> getHSGTCGDetail(String code) {
        String basicUrl = "http://dcfm.eastmoney.com/em_mutisvcexpandinterface/api/js/get?type=HSGTHDSTA&token=70f12f2f4f091e459a279469fe49eca5&filter=(SCODE=%27" + code + "%27)&st=HDDATE&sr=-1&p=1&ps=50&rt=53109038";
        //String url = basicUrl.replace("(","%28").replace(")","%29");
        String result = HttpUtil.doGet2(basicUrl);

        List<EastHsgtcgDetail> list = new ArrayList<>();
        JSONArray array = JSONArray.parseArray(result);
        for (int i = 0; i < array.size(); i++) {
            EastHsgtcgDetail ext = new EastHsgtcgDetail();
            JSONObject jsonObject = array.getJSONObject(i);
            ext.setCode(jsonObject.getString("SCODE"));
            ext.setName(jsonObject.getString("SNAME"));
            ext.setHddate(jsonObject.getString("HDDATE"));
            ext.setCloseprice(jsonObject.getString("CLOSEPRICE"));
            ext.setSharesrate(jsonObject.getBigDecimal("SHARESRATE"));
            ext.setShareholdsum(jsonObject.getBigDecimal("SHAREHOLDSUM"));
            ext.setShareholdprice(jsonObject.getString("SHAREHOLDPRICE"));
            ext.setShareholdpriceone(jsonObject.getBigDecimal("SHAREHOLDPRICEONE"));
            ext.setShareholdpricefive(jsonObject.getBigDecimal("SHAREHOLDPRICEFIVE"));
            ext.setShareholdpriceten(jsonObject.getBigDecimal("SHAREHOLDPRICETEN"));
            list.add(ext);
        }
        return list;
    }

    public static void main(String[] args) {
        EastHsgtcgSpider hsgtcgSpider = new EastHsgtcgSpider();
        List<EastHsgtcg> shareSZ = hsgtcgSpider.getHSGTCG(300, TradeDateUtil.getNearTradeDate(0), "ShareSZ");

        List<EastHsgtcgDetail> result = new ArrayList<>();
//        for (EastHsgtcg hsgtcg : shareSZ) {
//            try {
//                Thread.sleep(200);
//                List<EastHsgtcgDetail> hsgtcgDetail = hsgtcgSpider.getHSGTCGDetail(hsgtcg.getCode());
//
//                int continueBuyDaysCount = 0;
//                EastHsgtcgDetail firstDayDetail = null;
//                for (EastHsgtcgDetail detail : hsgtcgDetail) {
//                    if (continueBuyDaysCount == 0) {
//                        firstDayDetail = detail;
//                    }
//                    if (detail.getShareholdpriceone().intValue() < 0) {
//                        firstDayDetail.setContinueBuyDaysCount(continueBuyDaysCount);
//                        result.add(firstDayDetail);
//                        break;
//                    } else {
//                        continueBuyDaysCount++;
//                    }
//                }
//            } catch (RuntimeException | InterruptedException e) {
//                log.error("error:{},{}", hsgtcg.getCode(), hsgtcg.getName(), e);
//            }
//        }
//
//        String formatNum = MathUtil.formatNumWanYi("19121344.92");
//        System.out.println(formatNum);
//        String formatNum2 = MathUtil.formatNumBFB("0.0045543150543429");
//        System.out.println(formatNum2);
    }


}
